﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

namespace Customer_Profile.Admin
{
    public partial class GroupUser : System.Web.UI.Page
    {
        protected static string groupid;

        protected void Page_Load(object sender, EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    groupid = Request.QueryString["id"].ToString();
                }

                DataSet ds = GroupDao.getGroup(groupid);
                if (ds != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        txtUserGroupId.Value = ds.Tables[0].Rows[0]["group_id"].ToString();
                        txtUserGroupName.Value = ds.Tables[0].Rows[0]["group_name"].ToString();
                        txtDesc.Value = ds.Tables[0].Rows[0]["group_remark"].ToString();
                    }
                    else
                    {
                        txtUserGroupId.Value = "Null";
                        txtUserGroupName.Value = "Null";
                        txtDesc.Value = "Null";
                    }
                }

                //加载树
                TreeNode rootNode = new TreeNode();
                rootNode.Text = "MENU";
                rootNode.Value = "-1";
                rootNode.Expanded = true;
                treeMenu.Nodes.Add(rootNode);

                AddTree(0, rootNode);
            }
        }

        public void AddTree(int ParentID, TreeNode pNode)
        {
            DataSet ds = GroupDao.GetUserId(groupid);

            StringBuilder str = new StringBuilder();
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    str.Append(ds.Tables[0].Rows[i][1].ToString());
                    str.Append("_");
                }
            }

            DataView dvtree = UserDao.GetAllUserInfo(ParentID).Tables[0].DefaultView;
            foreach (DataRowView Row in dvtree)
            {
                TreeNode Node = new TreeNode();
                if (pNode == null)
                {
                    #region
                    Node.Text = Row["User_LoginID"].ToString();
                    string menuid = Row["User_Id"].ToString();
                    Node.Value = menuid;

                    Node.ShowCheckBox = true;
                    string strAllmenu = str.ToString();
                    if (strAllmenu.Length > 1)
                    {
                        strAllmenu = strAllmenu.Remove(strAllmenu.Length - 1, 1);

                        string[] strArray = strAllmenu.Split(new char[] { '_' });
                        for (int i = 0; i < strArray.Length; i++)
                        {
                            if (strArray[i] == menuid)
                                Node.Checked = true;
                        }
                    }
                    else
                    {
                        Node.Checked = false;
                    }
                    Node.Expanded = true;
                    treeMenu.Nodes.Add(Node);
                    AddTree(Convert.ToInt32(Row["User_id"].ToString()), Node);
                    #endregion
                }
                else
                {
                    #region
                    Node.Text = Row["User_LoginID"].ToString();
                    string menuid = Row["User_Id"].ToString();
                    Node.Value = menuid;
                    Node.ShowCheckBox = true;
                    Node.Expanded = true;
                    string strAllmenu = str.ToString();
                    if (strAllmenu.Length > 0)
                    {
                        strAllmenu = strAllmenu.Remove(strAllmenu.Length - 1, 1);
                        string[] strArray = strAllmenu.Split(new char[] { '_' });
                        for (int i = 0; i < strArray.Length; i++)
                        {
                            if (strArray[i] == menuid)
                                Node.Checked = true;
                        }
                    }
                    else
                    {
                        Node.Checked = false;
                    }
                    pNode.ChildNodes.Add(Node);
                    AddTree(Convert.ToInt32(Row["User_Id"].ToString()), Node);
                    #endregion
                }
            }
        }

        private void readAllNode(TreeNode node)
        {
            string menuid = node.Value;
            int mid = int.Parse(menuid);
            if (node.Checked == true)
            {
                if (UserDao.CheckIsExistUser(mid, groupid) == false)
                {
                    UserDao.AddR(groupid, mid);
                }
            }
            else
            {
                if (UserDao.CheckIsExistUser(mid, groupid) == true)
                {
                    UserDao.DelR(groupid, mid);
                }
            }
            if (node.ChildNodes.Count > 0)
            {
                foreach (TreeNode tn in node.ChildNodes)
                {
                    readAllNode(tn);
                }
            }
        }

        protected void btnSave_ServerClick(object sender, EventArgs e)
        {
            TreeNode tnd = treeMenu.Nodes[0];
            readAllNode(tnd);

            //记录用户操作日志
            //CVOC.Comm.WriteSystemLog(Session["SS_USER_ID"].ToString(), Utility.ReplaceChar(Utility.ReplaceQuote(Request["menu_idx"].ToString()), true), "03", Page.Request.UserHostAddress, Page.Request.Path, "用户组权限修改，用户组ID：" + txtUserGroupId.Value);
        }

        protected void btnBack_ServerClick(object sender, EventArgs e)
        {
            Response.Redirect("UserGroupManage.aspx", true);
        }
    }
}
